var utils = require("../lib/Utils");

exports.index = function (req,res,next) {
    utils.httpPost("/system/getUserMenus",{userId:req.session.userid},function (data) {
        if(data.code === "0"){
            req.session.menus = data.data;
            utils.render(req,res,"main",{});
        }else {
            req.session.menus = "";
            console.log("查询失败");
            utils.render(req,res,"main",{});
        }
    });
}

exports.menuInfo = function (req,res,next) {
    utils.render(req,res,"menu",{});
}

exports.addMenu = function (req,res,next) {
    var id = req.query.id;
    if(id == '' || id == undefined){
        utils.render(req,res,"addMenu",{});    
    }else {
        utils.httpPost("/system/getMenu",{id:id},function (data) {
            if(data.code === "0"){
                var menu = data.data;
                utils.render(req,res,"addMenu",{menu:JSON.stringify(menu)});
            }else {
                utils.render(req,res,"addMenu",{});
            }
        })
    }
    
}

exports.addMenus = function (req,res,next) {
    var param = {
        menuId:req.body.menuId,
        menuName:req.body.menuName,
        url:req.body.url,
        method:req.body.method,
        parentId:req.body.parentId,
        menuKey:req.body.menuKey,
        isDisplay:req.body.isDisplay,
        sort:req.body.sort,
        isLogin:req.body.isLogin,
        createUser:req.session.userid,
        updUser:req.session.userid
    }

    var retData = "";
    if(param.menuId == "") {
        utils.httpPost("/system/addMenu", param, function (data) {
            if (data.code === "0") {
                res.status(200).end(JSON.stringify({code: "0", msg: "添加成功", url: utils.menus(req, "main")}));
            } else {
                res.status(200).end(JSON.stringify({code: "-1", msg: "添加失败"}));
            }
        });
    }else {
        utils.httpPost("/system/updMenu", param, function (data) {
            if (data.code === "0") {
                res.status(200).end(JSON.stringify({code: "0", msg: "修改成功", url: utils.menus(req, "main")}));
            } else {
                res.status(200).end(JSON.stringify({code: "-1", msg: "修改失败"}));
            }
        });
    }
}

exports.getParent = function (req,res,next) {
    var param = {
        parentId:0,
        sign:1
    }

    utils.httpPost("/system/getMenus",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"添加成功",data:data.data}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"添加失败"}));
        }
    });
}

exports.getMenuList = function (req,res,next) {
    var param = {
        limit:req.body.limit,
        offset:req.body.offset
    }

    utils.httpPost("/system/getAllMenus",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"获取成功",rows:data.data.menus,total:data.data.cnt}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"获取失败"}));
        }
    })
}

exports.delMenus = function (req,res,next) {
    var ids = JSON.parse(req.body.ids);
    var param = {
        ids:ids.join(','),
        updUser:req.session.userid
    }

    utils.httpPost("/system/delMenus",param,function (data) {
        if(data.code === "0"){
            console.log(data);
            res.status(200).end(JSON.stringify({code:"0",msg:"删除成功"}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"删除成功"}));
        }
    })


}


exports.privilege = function (req,res,next) {
    utils.render(req,res,"privilege",{});
}

exports.getPriviList = function (req,res,next) {
    var param = {
        limit:req.body.limit,
        offset:req.body.offset
    }

    utils.httpPost("/system/getAllPrivi",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"获取成功",rows:data.data.privis,total:data.data.cnt}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"获取失败"}));
        }
    })
}

exports.getPriviDetail = function (req,res,next) {
    var param = {id:req.body.id};

    utils.httpPost("/system/getPriviDetail",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"获取成功",rows:data.data.privid,total:data.data.cnt}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"获取失败"}));
        }
    })
}

exports.getPmenus = function (req,res,next) {
    var param = {priviId:req.body.priviId};

    utils.httpPost("/system/getPmenus",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"获取成功",data:data.data}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"获取失败"}));
        }
    })
}

exports.addPrivi = function (req,res,next) {

    utils.render(req,res,"addPrivi",{priviId:req.query.id,priviName:req.query.priviName});
}

exports.addPrivis = function (req,res,next) {
    var param = {
        priviId:req.body.priviId,
        userId:req.session.userid,
        priviName:req.body.priviName,
        menuIds:req.body.menuIds
    }

    console.log(param);

    if(req.body.priviId == 0) {
        utils.httpPost("/system/insPrivi", param, function (data) {
            if (data.code === "0") {
                res.status(200).end(JSON.stringify({code: "0", msg: "提交成功"}));
            } else {
                res.status(200).end(JSON.stringify({code: "-1", msg: "提交失败"}));
            }
        });
    }else {
        utils.httpPost("/system/updPrivi", param, function (data) {
            if (data.code === "0") {
                res.status(200).end(JSON.stringify({code: "0", msg: "修改成功"}));
            } else {
                res.status(200).end(JSON.stringify({code: "-1", msg: "修改失败"}));
            }
        });
    }
}

exports.delPrivi = function (req,res,next) {
    var ids = JSON.parse(req.body.ids);
    var param = {
        priviId:ids.join(","),
        userId:req.session.userid
    }

    utils.httpPost("/system/delPrivi", param, function (data) {
        if (data.code === "0") {
            res.status(200).end(JSON.stringify({code: "0", msg: "删除成功"}));
        } else {
            res.status(200).end(JSON.stringify({code: "-1", msg: "删除失败"}));
        }
    });
}